package com.jtpay.sdk.base;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;

import com.jtpay.sdk.util.CustomProgressDialog;
import com.jtpay.sdk.util.SharedPreferencesManager;

import appUtils.AppUtil;
import butterknife.ButterKnife;

public abstract class BaseActivity extends AppCompatActivity {

    protected static SharedPreferencesManager mSharedPreferencesManager;
    private CustomProgressDialog progressDialog;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(setView());
        ButterKnife.bind(this);
        AppUtil.getInstance().pushAC(this);
        init();
        initData();
        initView();
    }

    protected abstract int setView();

    protected abstract void initData();

    protected abstract void initView();

    private void init() {
        initSharePreferences();
    }

    private void initSharePreferences() {
        if (mSharedPreferencesManager == null) {
            mSharedPreferencesManager = SharedPreferencesManager.getInstance(getApplicationContext());
        }
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        ButterKnife.unbind(this);
        AppUtil.getInstance().removeAC(this);
    }

    public void showProgress(final int resID) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                progressDialog = new CustomProgressDialog(BaseActivity.this, getResources().getString(resID));
                progressDialog.show();
            }
        });
    }

    public void dismisProgress() {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                if (progressDialog != null && progressDialog.isShowing()) {
                    progressDialog.dismiss();
                    progressDialog = null;
                }
            }
        });
    }
}
